package com.addcn.pushlibrary.huawei

import android.util.Log
import com.addcn.pushlibrary.LPush
import com.addcn.pushlibrary.bean.PMessage
import com.huawei.hms.push.HmsMessageService
import com.huawei.hms.push.RemoteMessage
import org.json.JSONException
import org.json.JSONObject


/**
 * author:luwenbo
 * created on date : 2019-05-14
 * content:
 * describe:
 */
class LHuaweiPushReceiver : HmsMessageService() {
    private var TAG = "LHuaweiPushReceiver"

    //服务端更新token回调方法
    override fun onNewToken(token: String?) {
        super.onNewToken(token)
        Log.i(TAG, "onToken token:$token")
    }

    //申请token失败回调方法
    override fun onTokenError(p0: Exception?) {
        super.onTokenError(p0)
        Log.i(TAG, "onTokenError ")

    }

    //接收透传消息方法
    override fun onMessageReceived(message: RemoteMessage?) {
        super.onMessageReceived(message)
        Log.i(TAG, "onMessageReceived ")
        val pMessage = getMessage(message)
        LPush.getInstance(this).onMsgReceiver(pMessage, false)
    }

    //发送上行消息成功回调方法
    override fun onMessageSent(msgId: String?) {
        super.onMessageSent(msgId)
        Log.i(TAG, "onMessageSent ")
    }

    //发送上行消息失败回调方法
    override fun onSendError(msgId: String?, exception: Exception?) {
        super.onSendError(msgId, exception)
        Log.i(TAG, "onSendError ")
    }

    //发送上行消息时如果使用了消息回执能力
    override fun onMessageDelivered(msgId: String?, exception: Exception?) {
        super.onMessageDelivered(msgId, exception)
        Log.i(TAG, "onMessageDelivered ")
    }
//    override fun onToken(context: Context?, token: String?) {
//        super.onToken(context, token)
//        LSharedUtil.putString(context!!, "l_huawei_token", token)
//        Log.i(TAG, "onToken token:$token")
//    }
//

//
//    override fun onPushMsg(context: Context?, msgBytes: ByteArray?, extras: Bundle?): Boolean {
//        val pMessage = getMessage(extras)
//        LPush.getInstance(context).onNotificationClicked(pMessage)
//        return super.onPushMsg(context, msgBytes, extras)
//
//    }
//
//    override fun onEvent(context: Context?, event: Event?, extras: Bundle?) {
//        super.onEvent(context, event, extras)
//        Log.i(TAG, "onEvent extras:$extras")
//        val pMessage = getMessage(extras)
//        //LPush.getInstance(context).onNotificationClicked(pMessage)
//    }

    private fun getMessage(message: RemoteMessage?): PMessage? {
        val pMessage = PMessage()
        val extra = message!!.data

        try {
            if (extra != null) {
                val extraObj = JSONObject(extra!!)
                pMessage.id = if (extraObj.getString("msg_id") == null) "" else extraObj.getString("msg_id")
                pMessage.url = extraObj.getString("url")
                pMessage.clean = extraObj.getString("clean_notification")
                pMessage.extra = if (extraObj.getString("extra") == null) "" else extraObj.getString("extra")
                pMessage.title = extraObj.getString("title")
                pMessage.content = extraObj.getString("content")
                pMessage.bigThumb = extraObj.getString("image")
                pMessage.url = extraObj.getString("url")
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }

        pMessage.fromType = PMessage.FROM_HUAWEI
        return pMessage
    }

}
